const { User } = require('./model/User');
const { UserType } = require('./model/UserType');
const { Department } = require('./model/Department');
const { Approval } = require('./model/Approval');
const { Attachment } = require('./model/Attachment');
const { CommunicationLog } = require('./model/CommunicationLog');
const { Location } = require('./model/Location');
const { Log } = require('./model/Log');
const { TaskAssignment } = require('./model/TaskAssignment');
const { WorkOrder } = require('./model/WorkOrder');
const { Workorderhistory } = require('./model/Workorderhistory');
const { Employee } = require('./model/Employee');
const { Resource } = require('./model/Resource');
const { MainMenu } = require('./model/Mainmenu');
const { MainMenuSubMenu } = require('./model/MainmenuSubmenu');
const { UserOperations } = require('./model/UserOperations');



// 建立关联，让员工表与部门表建立关联
Employee.belongsTo(Department, { foreignKey: 'DepartmentID' });

UserOperations.belongsTo(User, { foreignKey: 'user_id' });


// 建立关联
// 定义 User 模型与 Department 模型之间的关联
User.belongsTo(Department, { foreignKey: 'DepartmentID' });

// 定义 User 模型与 UserType 模型之间的关联
User.belongsTo(UserType, { foreignKey: 'UserTypeID' });


MainMenu.belongsToMany(Resource, {
    through: MainMenuSubMenu,
    foreignKey: 'MainMenuID',
    otherKey: 'SubMenuID'
});

Resource.belongsToMany(MainMenu, {
    through: MainMenuSubMenu,
    foreignKey: 'SubMenuID',
    otherKey: 'MainMenuID'
});

// 建立关联关系
TaskAssignment.belongsTo(Employee, { foreignKey: 'AssignedToUserID', as: 'assignedToUserName' });
TaskAssignment.belongsTo(WorkOrder, { foreignKey: 'WorkOrderID', as: 'workOrderData' });

// 建立关联
WorkOrder.belongsTo(Employee, {
    foreignKey: 'CreatedUserID', // 使用正确的外键名称
    as: 'createdUserName',
});

// 导出模型和数据库连接实例
module.exports = {
    User,
    UserType,
    Department,
    Log,
    Approval,
    CommunicationLog,
    Employee,
    Location,
    TaskAssignment,
    WorkOrder,
    Attachment,
    Workorderhistory,
    Resource,
    MainMenu,
    MainMenuSubMenu,
    UserOperations
};
